package com.sjsu.aws.primitve.client.configurationupdater;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import com.sjsu.aws.configurationMgmt.ConfigMgmt;
import com.sjsu.aws.configurationMgmt.ConfigMgmtServiceImpl;
import com.sjsu.aws.configurationMgmt.ConfigWatcherService;
import com.sjsu.aws.configurationMgmt.Watcher;
import com.sjsu.aws.configurationMgmt.WatcherRegisterServiceImpl;
import com.sjsu.aws.utils.SessionIdGenerator;

/**
 * 
 * @author Pawar Deepali
 * 
 */
public class ConfigurationWatcher implements ConfigWatcherService {
	
	private final static ScheduledExecutorService	scheduler	    = Executors
	                                                                        .newScheduledThreadPool(1);
	private boolean	                              startWatchingFlag	= false;
	
	private String	                              WatcherId;
	
	public boolean isStartWatchingFlag() {
		return this.startWatchingFlag;
	}
	
	public ConfigurationWatcher() {
		this.WatcherId = new SessionIdGenerator().generateId(10);
	}
	
	public void setStartWatchingFlag(boolean startWatchingFlag) {
		this.startWatchingFlag = startWatchingFlag;
	}
	
	public static void main(String[] args) throws Exception {
		long pollingPeriod = 10000;
		String configName = "cfgd12";
		ConfigurationWatcher configurationWatcher = new ConfigurationWatcher();
		configurationWatcher.startPolling(pollingPeriod, configName);
	}
	
	@Override
	public void startPolling(long pollingPeriod, String configName)
	        throws Exception {
		Watcher watcher = new Watcher();
		watcher.setWatcherName(WatcherId);
		watcher.setWatchFlag("true");
		WatcherRegisterServiceImpl watcherRegisterServiceImpl = new WatcherRegisterServiceImpl();
		watcherRegisterServiceImpl.addWatcher(watcher);
		
		System.out.println("Start Configuration Watcher");
		ConfigMgmtServiceImpl cfgService = new ConfigMgmtServiceImpl();
		pollDB(pollingPeriod, configName, cfgService);
	}
	
	@Override
	public void pollDB(long pollingPeriod, final String configName,
	        final ConfigMgmtServiceImpl cfgService) {
		final Runnable watcher = new Runnable() {
			public void run() {
				System.out
				        .println("Polling Configuration Management Table for Watcher...");
				// start polling registered watchers
				WatcherRegisterServiceImpl watcherRegisterServiceImpl;
				try {
					watcherRegisterServiceImpl = new WatcherRegisterServiceImpl();
					
					if (watcherRegisterServiceImpl.getRegisteredWatcher(
					        WatcherId).equalsIgnoreCase("true")) {
						ConfigMgmt cfg = new ConfigMgmt();
						cfg.setConfigId(configName);
						cfgService.readConfig(cfg);
						Watcher watcher = new Watcher();
						watcher.setWatcherName(WatcherId);
						watcher.setWatchFlag("false");
						watcherRegisterServiceImpl.updateWatchFlag(watcher);
					} else {
						System.out.println("Nothing is updated");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		
		final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(
		        watcher, 0, pollingPeriod, MILLISECONDS);
	}
	
}
